package com.mdd.maduoduo.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.ExchangeView;

import javax.inject.Inject;

public class ExchangeActivity extends BaseActivity implements ExchangeView {

    private ImageView ivBack;

    private TextView tvExchange;

    private EditText etCode;

    @Inject
    public LoginPresenter mPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exchange;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setExchangeView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        ivBack = findViewById(R.id.ivBack);
        etCode = findViewById(R.id.etCode);
        tvExchange = findViewById(R.id.tvExchange);
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvExchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                    String apiTid = etCode.getText().toString();
                    if (!apiTid.isEmpty()){
                        mPresenter.exchange(ExchangeActivity.this,apiTid);
                    }else {
                        ToastUtils.showLongToast(ExchangeActivity.this,"请输入兑换码");
                    }
                }
            }
        });
    }

    @Override
    public void renderSuccess(Response response) {
        if (response.code.equals("200")){
            ToastUtils.showLongToast(ExchangeActivity.this,"兑换成功");
            finish();
        }
    }
}
